using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;
using System.Net;

namespace TechChart
{
    struct Company
    {
        public string code;
        public string name;
    }
	/// <summary>
	/// Summary description for ctlMain.
	/// </summary>
	public class ctlMain : System.Windows.Forms.UserControl
    {
        private int nMarket;
        private string[] PriceBoardServices;
        private string[] MarketNames;
        private ComboBox cbMarkets;
        private ComboBox cbCompanies;
        private ComboBox cbRange;
        private ComboBox cbStyle;
        private WebBrowser wbPrice;
        private WebBrowser wbVolume;
        private WebBrowser wbMACD;
        private WebBrowser wbRSI;
        private WebBrowser wbMFI;
        private IContainer components;
        private List<Company> lstCompanies;
        private string[] ranges = { "1m", "3m", "6m", "1y", "2y", "5y" };
        private string PriceService;
        private string VolumeService;
        private string MACDService;
        private string RSIService;
        private Label lbCompanyName;
        private string MFIService;

		public ctlMain()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			// TODO: Add any initialization after the InitializeComponent call

		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.cbMarkets = new System.Windows.Forms.ComboBox();
            this.cbCompanies = new System.Windows.Forms.ComboBox();
            this.cbRange = new System.Windows.Forms.ComboBox();
            this.cbStyle = new System.Windows.Forms.ComboBox();
            this.lbCompanyName = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // cbMarkets
            // 
            this.cbMarkets.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.cbMarkets.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbMarkets.FormattingEnabled = true;
            this.cbMarkets.Location = new System.Drawing.Point(568, 15);
            this.cbMarkets.Name = "cbMarkets";
            this.cbMarkets.Size = new System.Drawing.Size(92, 21);
            this.cbMarkets.TabIndex = 4;
            this.cbMarkets.SelectedIndexChanged += new System.EventHandler(this.cbMarkets_SelectedIndexChanged);
            // 
            // cbCompanies
            // 
            this.cbCompanies.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.cbCompanies.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbCompanies.FormattingEnabled = true;
            this.cbCompanies.Location = new System.Drawing.Point(666, 15);
            this.cbCompanies.Name = "cbCompanies";
            this.cbCompanies.Size = new System.Drawing.Size(92, 21);
            this.cbCompanies.Sorted = true;
            this.cbCompanies.TabIndex = 5;
            this.cbCompanies.SelectedIndexChanged += new System.EventHandler(this.cbCompanies_SelectedIndexChanged);
            // 
            // cbRange
            // 
            this.cbRange.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.cbRange.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbRange.FormattingEnabled = true;
            this.cbRange.Items.AddRange(new object[] {
            "1 month",
            "3 months",
            "6 months",
            "1 year",
            "2 years",
            "5 years"});
            this.cbRange.Location = new System.Drawing.Point(764, 15);
            this.cbRange.Name = "cbRange";
            this.cbRange.Size = new System.Drawing.Size(92, 21);
            this.cbRange.TabIndex = 6;
            // 
            // cbStyle
            // 
            this.cbStyle.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.cbStyle.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbStyle.FormattingEnabled = true;
            this.cbStyle.Items.AddRange(new object[] {
            "line",
            "ohlc",
            "candle"});
            this.cbStyle.Location = new System.Drawing.Point(862, 15);
            this.cbStyle.Name = "cbStyle";
            this.cbStyle.Size = new System.Drawing.Size(92, 21);
            this.cbStyle.TabIndex = 7;
            // 
            // lbCompanyName
            // 
            this.lbCompanyName.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.lbCompanyName.AutoSize = true;
            this.lbCompanyName.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(163)));
            this.lbCompanyName.Location = new System.Drawing.Point(564, 42);
            this.lbCompanyName.Name = "lbCompanyName";
            this.lbCompanyName.Size = new System.Drawing.Size(57, 20);
            this.lbCompanyName.TabIndex = 8;
            this.lbCompanyName.Text = "label1";
            // 
            // ctlMain
            // 
            this.BackColor = System.Drawing.Color.White;
            this.Controls.Add(this.lbCompanyName);
            this.Controls.Add(this.cbStyle);
            this.Controls.Add(this.cbRange);
            this.Controls.Add(this.cbCompanies);
            this.Controls.Add(this.cbMarkets);
            this.Name = "ctlMain";
            this.Size = new System.Drawing.Size(1105, 748);
            this.Load += new System.EventHandler(this.ctlMain_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void ctlMain_Load(object sender, System.EventArgs e)
        {
            #region My controls region
            this.lstCompanies = new List<Company>();
            this.wbPrice = new System.Windows.Forms.WebBrowser();
            this.wbVolume = new System.Windows.Forms.WebBrowser();
            this.wbMACD = new System.Windows.Forms.WebBrowser();
            this.wbRSI = new System.Windows.Forms.WebBrowser();
            this.wbMFI = new System.Windows.Forms.WebBrowser();
            // 
            // wbRSI
            // 
            this.wbRSI.Location = new System.Drawing.Point(590, 60);
            this.wbRSI.MinimumSize = new System.Drawing.Size(20, 20);
            this.wbRSI.Name = "wbRSI";
            this.wbRSI.Size = new System.Drawing.Size(500, 150);
            this.wbRSI.TabIndex = 11;
            // 
            // wbMFI
            // 
            this.wbMFI.Location = new System.Drawing.Point(590, 180);
            this.wbMFI.MinimumSize = new System.Drawing.Size(20, 20);
            this.wbMFI.Name = "wbMFI";
            this.wbMFI.Size = new System.Drawing.Size(500, 150);
            this.wbMFI.TabIndex = 12;
            // 
            // wbMACD
            // 
            this.wbMACD.Location = new System.Drawing.Point(590, 320);
            this.wbMACD.MinimumSize = new System.Drawing.Size(20, 20);
            this.wbMACD.Name = "wbMACD";
            this.wbMACD.Size = new System.Drawing.Size(570, 140);
            this.wbMACD.TabIndex = 10;
            // 
            // wbPrice
            // 
            this.wbPrice.Location = new System.Drawing.Point(5, 10);
            this.wbPrice.MinimumSize = new System.Drawing.Size(20, 20);
            this.wbPrice.Name = "wbPrice";
            this.wbPrice.Size = new System.Drawing.Size(570, 380);
            this.wbPrice.TabIndex = 8;
            // 
            // wbVolume
            // 
            this.wbVolume.Location = new System.Drawing.Point(5, 380);
            this.wbVolume.MinimumSize = new System.Drawing.Size(20, 20);
            this.wbVolume.Name = "wbVolume";
            this.wbVolume.Size = new System.Drawing.Size(570, 130);
            this.wbVolume.TabIndex = 9;
            //
            // Main
            //
            this.Controls.Add(this.wbMACD);
            this.Controls.Add(this.wbVolume);
            this.Controls.Add(this.wbPrice);
            this.Controls.Add(this.wbRSI);
            this.Controls.Add(this.wbMFI);
            this.cbRange.SelectedIndex = 0;
            this.cbStyle.SelectedIndex = 0;
            #endregion

            // Initialize config
            StreamReader sr = new StreamReader("config.txt");
            while (!sr.EndOfStream)
            {
                string t = sr.ReadLine().Normalize();
                switch (t)
                {
                    case "[nMarkets]":
                        nMarket = int.Parse(sr.ReadLine());
                        PriceBoardServices = new string[nMarket];
                        MarketNames = new string[nMarket];
                        for (int i = 0; i < nMarket; i++)
                        {
                            MarketNames[i] = sr.ReadLine().Trim();
                            PriceBoardServices[i] = sr.ReadLine().Trim();
                            cbMarkets.Items.Add(MarketNames[i]);
                        }
                        break;
                    case "[Tech chart]":
                        // Price
                        PriceService = sr.ReadLine().Trim();
                        // Volume
                        VolumeService = sr.ReadLine().Trim();
                        // MACD
                        MACDService = sr.ReadLine().Trim();
                        // RSI
                        RSIService = sr.ReadLine().Trim();
                        // MFI
                        MFIService = sr.ReadLine().Trim();
                        break;
                }
            }
            sr.Close();
            cbMarkets.SelectedIndex = 0;
		}

        private void cbMarkets_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Load danh sach cong ty
            cbCompanies.Items.Clear();
            lstCompanies.Clear();
            StreamReader sr = new StreamReader(cbMarkets.Text + "\\listCompanies.txt");
            while (!sr.EndOfStream)
            {
                Company comp = new Company();
                comp.code = sr.ReadLine().Trim();
                comp.name = sr.ReadLine().Trim();

                cbCompanies.Items.Add(comp.code);
                lstCompanies.Add(comp);
            }
            sr.Close();
            cbCompanies.SelectedIndex = 0;
        }

        private void cbCompanies_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbCompanyName.Text = cbCompanies.Text + " - " + lstCompanies[cbCompanies.SelectedIndex].name;
            string path;
            // Price
            path = PriceService.Replace("<code>", cbCompanies.Text).Replace("<style>", cbStyle.Text).Replace("<range>", ranges[cbRange.SelectedIndex]);
            wbPrice.Navigate(path);

            // Volume
            path = VolumeService.Replace("<code>", cbCompanies.Text).Replace("<range>", ranges[cbRange.SelectedIndex]);
            wbVolume.Navigate(path);

            // MACD
            path = MACDService.Replace("<code>", cbCompanies.Text).Replace("<range>", ranges[cbRange.SelectedIndex]);
            wbMACD.Navigate(path);

            // RSI
            path = RSIService.Replace("<code>", cbCompanies.Text).Replace("<range>", ranges[cbRange.SelectedIndex]);
            wbRSI.Navigate(path);

            // MFI
            path = MFIService.Replace("<code>", cbCompanies.Text).Replace("<range>", ranges[cbRange.SelectedIndex]);
            wbMFI.Navigate(path);
        }
    }
}
